// pages/calc/calc.js
Page({
  data: {
    sub: '',    // 计算公式
    num: '0',    // 当前显示数字

  },
     // 状态标识
     numChangeFlag: false,  // 是否切换到第二个数字
     execFlag: false,       // 是否完成运算符输入
     resultFlag: false,     // 是否显示计算结果
     calcData: {            // 计算数据存储
       num1: 0,
       num2: null,
       op: null
     },
 

  // 数字按钮事件
  numBtn: function (e) {
    const num = e.currentTarget.dataset.val;
    let currentNum = this.data.num;

    // 处理结果状态
    if (this.resultFlag) {
      this.resetBtn();
      currentNum = '0';
    }

    // 处理运算符切换状态
    if (this.numChangeFlag) {
      this.numChangeFlag = false;
      currentNum = '0';
    }

    // 更新数字
    currentNum = currentNum === '0' ? num : currentNum + num;
    this.setData({ num: currentNum });
  },

  // 运算符按钮
  opBtn: function (e) {
    const op = e.currentTarget.dataset.val;
    const displayOp = {'×': '×', '÷': '÷', '+': '+', '-': '-'}[op];
    const realOp = {'×': '*', '÷': '/', '+': '+', '-': '-'}[op];
    
    if (this.calcData.num1 === null) {
      // 强制转换为浮点数然后计算
      this.calcData.num1 = parseFloat(this.data.num);
    } else if (!this.numChangeFlag) {
      this.calcData.num2 = parseFloat(this.data.num);
      this.calculate();
    }
    // op这里是计算符号
    this.calcData.op = realOp;
    // numChangeFlag标志符号
    this.numChangeFlag = true;
    this.execFlag = true;
    
    this.setData({
      sub: `${this.calcData.num1} ${displayOp} `,
      num: this.data.num
    });
  },

  // 等于按钮
  calculateBtn: function () {
    if (this.calcData.op && !this.numChangeFlag) {
      this.calcData.num2 = parseFloat(this.data.num);
      this.calculate();
      this.resultFlag = true;
    }
  },

  // 执行计算
  calculate: function () {
    let result;
    const { num1, num2, op } = this.calcData;
    switch (op) {
      case '+': result = num1 + num2; break;
      case '-': result = num1 - num2; break;
      case '*': result = num1 * num2; break;
      case '/': result = num2 !== 0 ? num1 / num2 : 'Error'; break;
    }
    
    this.setData({
      sub: `${num1} ${op} ${num2} = `,
      num: result.toString()
    });

    this.calcData.num1 = result;
    this.numChangeFlag = true;
    this.execFlag = false;
  },

  // 复位按钮
  resetBtn: function () {
    this.setData({
      sub: '',
      num: '0'
    });
    this.calcData = { num1: null, num2: null, op: null };
    this.numChangeFlag = false;
    this.execFlag = false;
    this.resultFlag = false;
  },

  // 小数点按钮
  dotBtn: function () {
    if (!this.data.num.includes('.')) {
      this.setData({ num: this.data.num + '.' });
    }
  },

  // 删除按钮
  delBtn: function () {
    const newNum = this.data.num.slice(0, -1) || '0';
    this.setData({ num: newNum.length ? newNum : '0' });
  },

  // 正负号按钮
  negBtn: function () {
    let newNum;
    if(this.data.num > 0){
      newNum = `-${this.data.num}`
    }else if(this.data.num < 0){
      newNum = this.data.num.substr(1)
    }else if(this.data.num==0 || this.data.num == 0.0){
        // 如果是0 或者 0.0 那么就不做操作
    }
    this.setData({ num: newNum });
  }
})